package com.beilang.hotline.company.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.beilang.base.core.dao.PaginationBaseDAO;
import com.beilang.base.pagination.PaginationHelper;
import com.beilang.base.util.StringUtil;
import com.beilang.hotline.company.model.CompanyModel;
import com.beilang.hotline.company.to.SearchTO;

public class CompanyDAO extends PaginationBaseDAO{
	
	public CompanyModel createCompany(CompanyModel companyModel) {
        companyModel.setDeleteInd("N");
        return (CompanyModel)this.save(companyModel);
	}
	
	public CompanyModel saveCompany(CompanyModel companyModel) {
		return (CompanyModel)this.save(companyModel);
	}
	
	public void deleteCompany(Long companyId){
		CompanyModel company= this.getCompany(companyId);
        company.setDeleteInd("Y");
        this.save(company);
	}
	
	public CompanyModel getCompany(Long companyId){
		return (CompanyModel)this.get(CompanyModel.class, companyId);
	}
	
	public Integer searchCount(SearchTO searchTO) {
        Map map = new HashMap();
        setCompanyTypeMap(map,searchTO.getAccessCompanyList());
        String companyName=StringUtil.convertEmpty2Null(searchTO.getCompanyName());
        if(companyName!=null)companyName="%"+companyName+"%";
        map.put("companyName", companyName);
        String companyNumber=StringUtil.convertEmpty2Null(searchTO.getCompanyNumber());
        if(companyNumber!=null)companyNumber="%"+companyNumber+"%";
        map.put("companyNumber", companyNumber);        
        return (Integer) getSqlMapClientTemplate().queryForObject("searchCompanyCount", map);
    }

    public List searchList(PaginationHelper helper, SearchTO searchTO) {
        Map map = new HashMap();
        setCompanyTypeMap(map,searchTO.getAccessCompanyList());
        String companyName=StringUtil.convertEmpty2Null(searchTO.getCompanyName());
        if(companyName!=null)companyName="%"+companyName+"%";
        map.put("companyName", companyName);
        String companyNumber=StringUtil.convertEmpty2Null(searchTO.getCompanyNumber());
        if(companyNumber!=null)companyNumber="%"+companyNumber+"%";
        map.put("companyNumber", companyNumber);  
        map = constructPaginationMap(helper, map);
        return getSqlMapClientTemplate().queryForList("searchCompanyList", map);
    }
     public List searchList(SearchTO searchTO) {
        Map map = new HashMap();
         setCompanyTypeMap(map,searchTO.getAccessCompanyList());
        String companyName=StringUtil.convertEmpty2Null(searchTO.getCompanyName());
        if(companyName!=null)companyName="%"+companyName+"%";
        map.put("companyName", companyName);
        String companyNumber=StringUtil.convertEmpty2Null(searchTO.getCompanyNumber());
        if(companyNumber!=null)companyNumber="%"+companyNumber+"%";
        map.put("companyNumber", companyNumber);
        return getSqlMapClientTemplate().queryForList("searchCompanyAllList", map);
    }

    protected  String mapSortField(String dataField){
    	if(StringUtil.isNullOrBlank(dataField))return dataField;
    	if(dataField.equalsIgnoreCase("companyNumber")) return "company_number";
    	if(dataField.equalsIgnoreCase("companyName")) return "company_name";
    	if(dataField.equalsIgnoreCase("customArea")||dataField.equalsIgnoreCase("customAreaDesc")) return "custom_area";
    	if(dataField.equalsIgnoreCase("companyCode")) return "company_code";
    	if(dataField.equalsIgnoreCase("contactIp")) return "contact_ip";
    	if(dataField.equalsIgnoreCase("companyServiceLevel")||dataField.equalsIgnoreCase("companyServiceLevelDesc")) return "company_service_level";
    	return dataField;
    }

    private Map setCompanyTypeMap(Map map, List companyTypeList) {
        if (companyTypeList != null && companyTypeList.size() > 0) {
            StringBuffer sysWhereClause = new StringBuffer("(");
            for (Object aList : companyTypeList) {
                String s = (String) aList;
                if (!StringUtil.isNullOrBlank(s)) {
                    if (!sysWhereClause.toString().equalsIgnoreCase("(")) {
                        sysWhereClause.append(" or ");
                    }
                    sysWhereClause.append("(");
                    sysWhereClause = sysWhereClause.append("company_type = '");
                    sysWhereClause = sysWhereClause.append(s);
                    sysWhereClause = sysWhereClause.append("' or ");
                    sysWhereClause = sysWhereClause.append("company_type like '");
                    sysWhereClause = sysWhereClause.append(s).append("|").append("%'");
                    sysWhereClause = sysWhereClause.append(" or ");
                    sysWhereClause = sysWhereClause.append("company_type like '%");
                    sysWhereClause = sysWhereClause.append("|").append(s).append("|%'");
                    sysWhereClause = sysWhereClause.append(" or ");
                    sysWhereClause = sysWhereClause.append("company_type like '%");
                    sysWhereClause = sysWhereClause.append("|").append(s).append("'");
                    sysWhereClause = sysWhereClause.append(")");
                }
            }
            String companyType = sysWhereClause.toString();
            companyType = companyType + ")";
            if (!"()".equalsIgnoreCase(companyType)) {
                map.put("companyType", companyType);
            }
        }
        return map;
    }
}
